package com.base.doublepoint;

import java.util.HashMap;

/**
 * 8. 字符串转换整数 (atoi)
 *
 * @author li
 */
public class MyAtoi {
    public int myAtoi(String s) {
        HashMap<Character, Integer> integerHashMap = new HashMap<>();
        initMap(integerHashMap);
        s = s.replaceAll(" ", "");
        char[] chars = s.toCharArray();
        long result = 0;
        long type = 1;
        for (int i = 0; i < chars.length; i++) {
            if (integerHashMap.containsKey(chars[i])) {
                result = result * 10 + integerHashMap.get(chars[i]);
            } else if (chars[i] == '+') {
                continue;
            } else if (chars[i] == '-') {
                type *= -1;
            }else {
                break;
            }
        }
        result *=type;
        System.out.println(result);
        return (int) result;
    }

    private void initMap(HashMap<Character, Integer> map) {
        map.put('0', 0);
        map.put('1', 1);
        map.put('2', 2);
        map.put('3', 3);
        map.put('4', 4);
        map.put('5', 5);
        map.put('6', 6);
        map.put('7', 7);
        map.put('8', 8);
        map.put('9', 9);
    }


    public static void main(String[] args) {
        MyAtoi myAtoi = new MyAtoi();
        System.out.println(myAtoi.myAtoi("1234a44444444444444"));
    }

}
